package com.yyx.app1.Question_function.Control.ChatListView;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.yyx.app1.Data.Question.Chat_Data;
import com.yyx.app1.Data.Question.Question_Activity_Data;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.MyApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuexing on 2015/9/24.
 */
public class ListView_Chat_Adapter extends BaseAdapter {
    private Context context;
    private List<Chat_Data> list;
    private Question_Activity_Data data;
    private String OtherPortrait;
    private final int HEAD = 0;
    private final int OTHER = 1;
    private final int MY = 2;


    public ListView_Chat_Adapter(Context context) {
        this.context = context;
    }

    public void findAll()
    {
        list=new ArrayList<>();
        list.add(new Chat_Data("maomaoid","myid", Chat_Data.Chat_Type.IMAGE,null,new testDate().strings[5],null));
        list.add(new Chat_Data("myid","maomaoid", Chat_Data.Chat_Type.TEXT,"知道了,这个情况很严重,你来一趟我的诊所做一个详细检查更稳妥.",null,null));
    }

    public void findAll(Question_Activity_Data data)
    {
        //问题展示
        this.data = data;
        //历史记录
        this.OtherPortrait = data.getPortraitURL();
    }

    public void add(Chat_Data chat_data)
    {
        list.add(chat_data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size()+1;
    }

    @Override
    public Object getItem(int position) {
        if (position==0)
            return null;
        else
        {
            return list.get(position-1);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        Chat_Data chat_data = null;
        if (position>0)
            chat_data = list.get(position-1);
        String id = ((MyApp) ((Activity) context).getApplication()).getBossInfo().getId();
        if (position==0)
            return HEAD;
        else if (chat_data.getSendId()== id)
        {
            return MY;
        }
        else
        {
            return OTHER;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemVIew_Chat_Head head;
        ItemView_ChatListView_my my;
        ItemView_ChatListView_other other;
        Chat_Data chat_data = null;
        if (position>0)
            chat_data = list.get(list.size()-position);
        String id = ((MyApp) ((Activity) context).getApplication()).getBossInfo().getId();
        convertView = null;
        if (position == 0)
        {
            head = ItemVIew_Chat_Head_.build(context);
            head.bind(data);
            return head;
        }
        else if (chat_data.getSendId()==id)
        {
            my = ItemView_ChatListView_my_.build(context);
            if (chat_data.getType()== Chat_Data.Chat_Type.TEXT)
            {
                my.bind_SendText(chat_data.getTextContent());
            }
            else
            {
                if (chat_data.getImageUrl()==null)
                {
                    my.bind_SendImage(chat_data.getImageUri());
                }
                else
                {
                    my.bind_SendImage(chat_data.getImageUrl());
                }
            }
            return my;
        }
        else
        {
            other = ItemView_ChatListView_other_.build(context);
            if (chat_data.getType() == Chat_Data.Chat_Type.TEXT)
            {
                other.bind_SendText(data.getPortraitURL(),chat_data.getTextContent());
            }
            else
            {
                if (chat_data.getImageUrl()==null)
                {
                    other.bind_SendImage(data.getPortraitURL(),chat_data.getImageUri());
                }
                else
                {
                    other.bind_SendImage(data.getPortraitURL(),chat_data.getImageUrl());
                }
            }
            return other;
        }

//        else
//        {
//            if (position == 0)
//            {
//                head = (ItemVIew_Chat_Head)convertView;
//                head.bind(data);
//                return head;
//            }
//            else if (chat_data.getSendId()==id)
//            {
//                if (getItemViewType(position)==MY)
//                my = (ItemView_ChatListView_my)convertView;
//                if (chat_data.getType()== Chat_Data.Chat_Type.TEXT)
//                {
//                    my.bind_SendText(chat_data.getTextContent());
//                }
//                else
//                {
//                    if (chat_data.getImageUrl()==null)
//                    {
//                        my.bind_SendImage(chat_data.getImageUri());
//                    }
//                    else
//                    {
//                        my.bind_SendImage(chat_data.getImageUrl());
//                    }
//                }
//                return my;
//            }
//            else
//            {
//                other = (ItemView_ChatListView_other)convertView;
//                if (chat_data.getType() == Chat_Data.Chat_Type.TEXT)
//                {
//                    other.bind_SendText(data.getPortraitURL(),chat_data.getTextContent());
//                }
//                else
//                {
//                    if (chat_data.getImageUrl()==null)
//                    {
//                        other.bind_SendImage(data.getPortraitURL(),chat_data.getImageUri());
//                    }
//                    else
//                    {
//                        other.bind_SendImage(data.getPortraitURL(),chat_data.getImageUrl());
//                    }
//                }
//                return other;
//            }
//        }
    }
}
